package com.ruoyi.system.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Author: lsn
 * @ClassName: JdbcUtils
 * @Description:
 * @Data: 2023/7/13 15:28
 **/
@Component
public class JdbcUtils {

    @Autowired
    private Environment environment;

    public HashMap<String, String> getDBInfo() {
        String url = environment.getProperty("spring.datasource.druid.url");
        String username = environment.getProperty("spring.datasource.druid.username");
        String password = environment.getProperty("spring.datasource.druid.password");
        String[] split = url.split(":");
        String host = String.format("%s:%s:%s", split[0], split[1], split[2]);
        String[] portSplit = split[3].split("/");
        String port = portSplit[0];

        String[] databaseSplit = portSplit[1].split("\\?");
        String dbName = databaseSplit[0];
        HashMap<String, String> result = new HashMap<>();
        result.put("url", url);
        result.put("host",host);
        result.put("port",port);
        result.put("dbName",dbName);
        result.put("userName",username);
        result.put("passWord",password);
        return result;
    }

}
